﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClassStyle
{
    public class PLCAction
    {
        public PLCAction Plc { get; set; }
        public string Path { get; }
        public PLCAction() { }
        public PLCAction(string path)
        {
            Path = path;
        }        
        List<DataTemplete> list = new List<DataTemplete>();
        // 创建 Modbus 客户端
       
        public void Exec()//心跳
        {            
                Task.Run(() => {
                    //心跳，稍后做
                    //while (true)
                    //{
                    //    foreach (DataTemplete item in list)
                    //    {
                    //        if (Convert.ToBoolean(FileHelper2025.Get_ini_data(item.InitTitleInfo, item.PlcAddress, Path + "\\plc.ini")))
                    //        {
                    //            //去调用班级增加
                    //            //item.OperateMethod();
                    //            item.OperateMethod.Invoke();
                    //        }
                    //    }
                    //    Console.WriteLine("心跳.............................");
                    //    Task.Delay(300).Wait();
                    //}

                    while (true)
                    {
                        //读取plc的数据  0-5
                        //bool[] arr = null;
                        foreach (DataTemplete item in list)
                        {

                            if (ModbusHelper.Instance.Get_modbus_data(item.PlcAddress, Path))
                            {
                                Console.WriteLine("处理逻辑............................." + item.PlcAddress);
                                //去调用班级增加
                                //item.OperateMethod();
                                item.OperateMethod.Invoke();
                            }
                        }
                        Task.Delay(500).Wait();
                    }

                });
        }
         public void AddTemplete(DataTemplete temp)//把班级和学生的增删改都放入这个方法内
        {
            list.Add(temp);
        }
    }
}
